package com.chouxiaozi.mybatisdruid.controller;


import com.chouxiaozi.mybatisdruid.dao.OrderMapper;
import com.chouxiaozi.mybatisdruid.entity.Order;
import com.chouxiaozi.mybatisdruid.entity.SysUser;
import com.chouxiaozi.mybatisdruid.service.SysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RestController
public class TestController {
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private OrderMapper orderMapper;
    Logger logger = LoggerFactory.getLogger(TestController.class);

    @RequestMapping("/hello")
    public String sayHello() {
        return "hello gouzi";
    }

    @RequestMapping("/get")
    public SysUser get(String id) {
        Long userId = Long.valueOf(id);
        SysUser sysUser = sysUserService.get(userId);
        return sysUser;
    }

    @Autowired
    private DataSource dataSource;

    @RequestMapping("/getName")
    public String getUsername() {
        logger.info("查询姓名");
        logger.error("ggggggggg");
        String name = null;
        String sql = "select user.name from tab_user user where user.id = 1";
        try {
            ResultSet rs = dataSource.getConnection().createStatement().executeQuery(sql);
            if (rs.next()) {
                name = rs.getString("name");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return name;
    }

    @GetMapping("/order/list")
    public List<Order> list(){
        Map map = new HashMap();
        map.put("goodId", 1);
        return orderMapper.queryOrderList2(map);
    }
}
